<?php


/*
	[UCenter Home] (C) 2007-2008 Comsenz Inc.
	$Id: cp_blog.php 13026 2009-08-06 02:17:33Z liguode $
*/

if (!defined('IN_UCHOME')) {
	exit ('Access Denied');
}

//检查信息
$picid = empty ($_GET['picid']) ? 0 : intval($_GET['picid']);
$pic = array ();
if ($picid) {
	$query = $_SGLOBAL['db']->query("SELECT * FROM " . tname('product') . " b 		
			WHERE b.picid='$picid'");

	$pic = $_SGLOBAL['db']->fetch_array($query);
	
	$pic[thumbpath] = empty($pic[thumbpath])?$pic[filepath] . ".thumb.jpg":$pic[thumbpath];
//	print_r($pic);
}
function create_dir($path) { //创建目录
	if (is_dir($path))
		return;

	$dirs = explode('/', $path);

	//判断操作系统，linux的话，去除第一空项
	if (strpos($path, '/') == 0) {
		$dirpath = '/';
		unset ($dirs[0]);
	}
	while (($directory = array_shift($dirs))) {
		$dirpath .= $directory . '/';
		if (!is_dir($dirpath)) {
			mkdir($dirpath);
			if (!chmod($dirpath, 0777)) {
				die('mkdir fail');
			}
		}
	}
}

function move($tmp_name, $new_name) {
	if ($tmp_name == $new_name) {
		return true;
	}
	if (@ copy($tmp_name, $new_name)) {
		@ unlink($tmp_name);
	}
	elseif ((function_exists('move_uploaded_file') && @ move_uploaded_file($tmp_name, $new_name))) {

	}
	elseif (@ rename($tmp_name, $new_name)) {

	} else {

		return false;
	}
	return true;
}
if ($_GET['op'] == 'delete') {
	//删除
	if (submitcheck('deletesubmit')) {
		include_once (S_ROOT . './source/function_delete.php');
		if ($ps = deleteproducts(array (
				$picid
			))) {
			foreach($ps as $v){
				@unlink(S_ROOT."./".$v[filepath]);
				@unlink(S_ROOT."./".$v[filepath].".thumb.jpg");
			}
			showmessage('do_success', "space.php?uid=$pic[uid]&do=product");
		} else {
			showmessage('failed_to_delete_operation');
		}
	}

}
if ($_POST["op"] == "move") {

	if (empty ($_POST['name'])) {
		showmessage("请填写产品名称");
	}
	if (empty ($_POST['filename'])) {
		showmessage("请选择需要上传的图片");
	}
	if (empty ($_POST['title'])) {
		showmessage("请输入图片描述");
	}
	$filepath = S_ROOT . "/swfupload/product/";
	move(S_ROOT . "/" . $_POST["file"], $filepath . $_POST["filename"]);
	move(S_ROOT . "/" . $_POST["thumb"], $filepath . $_POST["filename"] . ".thumb.jpg");
	if ($_POST["filename"] != $pic[filename]) {
		unlink($filepath . "/" . $pic[filename]);
		unlink($filepath . "/" . $pic[filename] . ".thumb.jpg");
	}
	
	
	move(S_ROOT . "./360upload/" . $_POST["thumb"], $filepath . $_POST["thumb"]);
	if ($_POST["thumb"] != $pic[thumbpath]) {
		unlink($filepath . "/" . $pic[thumb]);
	}
	$cat = trim($_POST[cat]);
	$cats = explode(",",$cat);

	if(count($cats)==1){
		$_POST[cat] = $cats[0];
	}elseif(count($cats)==2){
		$_POST[cat] = $cats[0];
		$_POST[subcat] = $cats[1];
	}


	$blogarr = array (
		"uid" => $_SGLOBAL['supe_uid'],
		"username" => $_SGLOBAL['supe_username'],
		"postip" => getonlineip(),
		"filename" => $_POST[filename],
		"name" => $_POST[name],
		"filepath" => "swfupload/product/" . $_POST["filename"],
		"thumbpath" => "swfupload/product/" . $_POST["thumb"],
		"title" => $_POST[title],
		"cat" => $_POST[cat],
		"subcat" => $_POST[subcat],
		"dateline" => $_SGLOBAL['timestamp']
	);

//print_r($blogarr);
//print_r($_POST);
//die();
	if ($_GET['picid']) {
		//更新	
		$picid = $_GET['picid'];
		updatetable('product', $blogarr, array (
			'picid' => $picid
		));
	} else {
		$picid = inserttable('product', $blogarr, 1);
	}
	showmessage("操作完成", "cp.php?ac=product&picid=" . $picid, 3);
}
//随机临时文件夹
$temp_dir = md5($_SGLOBAL['timestamp'] . rand(1, 10000));

//分类文件
include_once (S_ROOT . './source/function_product.php');
include_once template("cp_product");
?>